package com.itheima.controller;


import com.itheima.common.R;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
     String phone=user.getPhone();
     if(StringUtils.isNotEmpty(phone)){
         //生成4位随机验证码
         String code = ValidateCodeUtils.generateValidateCode(4).toString();

         //调用阿里云的短信服务
       //  SMSUtils.sendMessage("瑞吉外卖”，” “，phone,code);
         //将验证码保存起来
         session.setAttribute(phone,code);
         return R.success("手机发生成功");
     }


       return R.error("发送失败");
    }
}
